home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / lib / perl5 / perl5db.pl < prev    next >
Perl Script  |  1995-07-02  |  15KB  |  570 lines

  1. package DB;
  2.  
  3. # modified Perl debugger, to be run from Emacs in perldb-mode
  4. # Ray Lischner (uunet!mntgfx!lisch) as of 5 Nov 1990
  5. # Johan Vromans -- upgrade to 4.0 pl 10
  6.  
  7. $header = '$RCSfile: perl5db.pl,v $$Revision: 4.1 $$Date: 92/08/07 18:24:07 $';
  8. #
  9. # This file is automatically included if you do perl -d.
  10. # It's probably not useful to include this yourself.
  11. #
  12. # Perl supplies the values for @line and %sub.  It effectively inserts
  13. # a &DB'DB(<linenum>); in front of every place that can
  14. # have a breakpoint.  It also inserts a do 'perldb.pl' before the first line.
  15. #
  16. # $Log:    perldb.pl,v $
  17.  
  18. local($^W) = 0;
  19.  
  20. if (-e "/dev/tty") {
  21.     $console = "/dev/tty";
  22.     $rcfile=".perldb";
  23. }
  24. elsif (-e "con") {
  25.     $console = "con";
  26.     $rcfile="perldb.ini";
  27. }
  28. else {
  29.     $console = "sys\$command";
  30.     $rcfile="perldb.ini";
  31. }
  32.  
  33. open(IN, "<$console") || open(IN,  "<&STDIN");    # so we don't dingle stdin
  34. open(OUT,">$console") || open(OUT, ">&STDERR")
  35.     || open(OUT, ">&STDOUT");    # so we don't dongle stdout
  36. select(OUT);
  37. $| = 1;                # for DB::OUT
  38. select(STDOUT);
  39. $| = 1;                # for real STDOUT
  40. $sub = '';
  41.  
  42. # Is Perl being run from Emacs?
  43. $emacs = $main::ARGV[0] eq '-emacs';
  44. shift(@main::ARGV) if $emacs;
  45.  
  46. $header =~ s/.Header: ([^,]+),v(\s+\S+\s+\S+).*$/$1$2/;
  47. print OUT "\nLoading DB routines from $header\n";
  48. print OUT ("Emacs support ",
  49.        $emacs ? "enabled" : "available",
  50.        ".\n");
  51. print OUT "\nEnter h for help.\n\n";
  52.  
  53. sub DB {
  54.     &save;
  55.     ($package, $filename, $line) = caller;
  56.     $usercontext = '($@, $!, $,, $/, $\, $^W) = @saved;' .
  57.     "package $package;";        # this won't let them modify, alas
  58.     local(*dbline) = "::_<$filename";
  59.     $max = $#dbline;
  60.     if (($stop,$action) = split(/\0/,$dbline{$line})) {
  61.     if ($stop eq '1') {
  62.         $signal |= 1;
  63.     }
  64.     else {
  65.         $evalarg = "\$DB::signal |= do {$stop;}"; &eval;
  66.         $dbline{$line} =~ s/;9($|\0)/$1/;
  67.     }
  68.     }
  69.     if ($single || $trace || $signal) {
  70.     if ($emacs) {
  71.         print OUT "\032\032$filename:$line:0\n";
  72.     } else {
  73.         $prefix = $sub =~ /'|::/ ? "" : "${package}::";
  74.         $prefix .= "$sub($filename:";
  75.         if (length($prefix) > 30) {
  76.         print OUT "$prefix$line):\n$line:\t",$dbline[$line];
  77.         $prefix = "";
  78.         $infix = ":\t";
  79.         }
  80.         else {
  81.         $infix = "):\t";
  82.         print OUT "$prefix$line$infix",$dbline[$line];
  83.         }
  84.         for ($i = $line + 1; $i <= $max && $dbline[$i] == 0; ++$i) {
  85.         last if $dbline[$i] =~ /^\s*(}|#|\n)/;
  86.         print OUT "$prefix$i$infix",$dbline[$i];
  87.         }
  88.     }
  89.     }
  90.     $evalarg = $action, &eval if $action;
  91.     if ($single || $signal) {
  92.     $evalarg = $pre, &eval if $pre;
  93.     print OUT $#stack . " levels deep in subroutine calls!\n"
  94.         if $single & 4;
  95.     $start = $line;
  96.       CMD:
  97.     while ((print OUT "  DB<", $#hist+1, "> "), $cmd=&gets) {
  98.         {
  99.         $single = 0;
  100.         $signal = 0;
  101.         $cmd eq '' && exit 0;
  102.         chop($cmd);
  103.         $cmd =~ s/\\$// && do {
  104.             print OUT "  cont: ";
  105.             $cmd .= &gets;
  106.             redo CMD;
  107.         };
  108.         $cmd =~ /^q$/ && exit 0;
  109.         $cmd =~ /^$/ && ($cmd = $laststep);
  110.         push(@hist,$cmd) if length($cmd) > 1;
  111.         ($i) = split(/\s+/,$cmd);
  112.         eval "\$cmd =~ $alias{$i}", print OUT $@ if $alias{$i};
  113.         $cmd =~ /^h$/ && do {
  114.             print OUT "
  115. T        Stack trace.
  116. s        Single step.
  117. n        Next, steps over subroutine calls.
  118. r        Return from current subroutine.
  119. c [line]    Continue; optionally inserts a one-time-only breakpoint 
  120.         at the specified line.
  121. <CR>        Repeat last n or s.
  122. l min+incr    List incr+1 lines starting at min.
  123. l min-max    List lines.
  124. l line        List line;
  125. l        List next window.
  126. -        List previous window.
  127. w line        List window around line.
  128. l subname    List subroutine.
  129. f filename    Switch to filename.
  130. /pattern/    Search forwards for pattern; final / is optional.
  131. ?pattern?    Search backwards for pattern.
  132. L        List breakpoints and actions.
  133. S        List subroutine names.
  134. t        Toggle trace mode.
  135. b [line] [condition]
  136.         Set breakpoint; line defaults to the current execution line; 
  137.         condition breaks if it evaluates to true, defaults to \'1\'.
  138. b subname [condition]
  139.         Set breakpoint at first line of subroutine.
  140. d [line]    Delete breakpoint.
  141. D        Delete all breakpoints.
  142. a [line] command
  143.         Set an action to be done before the line is executed.
  144.         Sequence is: check for breakpoint, print line if necessary,
  145.         do action, prompt user if breakpoint or step, evaluate line.
  146. A        Delete all actions.
  147. V [pkg [vars]]    List some (default all) variables in package (default current).
  148. X [vars]    Same as \"V currentpackage [vars]\".
  149. < command    Define command before prompt.
  150. > command    Define command after prompt.
  151. ! number    Redo command (default previous command).
  152. ! -number    Redo number\'th to last command.
  153. H -number    Display last number commands (default all).
  154. q or ^D        Quit.
  155. p expr        Same as \"print DB::OUT expr\" in current package.
  156. = [alias value]    Define a command alias, or list current aliases.
  157. command        Execute as a perl statement in current package.
  158.  
  159. ";
  160.             next CMD; };
  161.         $cmd =~ /^t$/ && do {
  162.             $trace = !$trace;
  163.             print OUT "Trace = ".($trace?"on":"off")."\n";
  164.             next CMD; };
  165.         $cmd =~ /^S$/ && do {
  166.             foreach $subname (sort(keys %sub)) {
  167.             print OUT $subname,"\n";
  168.             }
  169.             next CMD; };
  170.         $cmd =~ s/^X\b/V $package/;
  171.         $cmd =~ /^V$/ && do {
  172.             $cmd = "V $package"; };
  173.         $cmd =~ /^V\b\s*(\S+)\s*(.*)/ && do {
  174.             local ($savout) = select(OUT);
  175.             $packname = $1;
  176.             @vars = split(' ',$2);
  177.             do 'dumpvar.pl' unless defined &main::dumpvar;
  178.             if (defined &main::dumpvar) {
  179.             &main::dumpvar($packname,@vars);
  180.             }
  181.             else {
  182.             print DB::OUT "dumpvar.pl not available.\n";
  183.             }
  184.             select ($savout);
  185.             next CMD; };
  186.         $cmd =~ /^f\b\s*(.*)/ && do {
  187.             $file = $1;
  188.             if (!$file) {
  189.             print OUT "The old f command is now the r command.\n";
  190.             print OUT "The new f command switches filenames.\n";
  191.             next CMD;
  192.             }
  193.             if (!defined $main::{'_<' . $file}) {
  194.             if (($try) = grep(m#^_<.*$file#, keys %main::)) {
  195.                 $file = substr($try,2);
  196.                 print "\n$file:\n";
  197.             }
  198.             }
  199.             if (!defined $main::{'_<' . $file}) {
  200.             print OUT "There's no code here anything matching $file.\n";
  201.             next CMD;
  202.             }
  203.             elsif ($file ne $filename) {
  204.             *dbline = "::_<$file";
  205.             $max = $#dbline;
  206.             $filename = $file;
  207.             $start = 1;
  208.             $cmd = "l";
  209.             } };
  210.         $cmd =~ /^l\b\s*([':A-Za-z_][':\w]*)/ && do {
  211.             $subname = $1;
  212.             $subname = "main::" . $subname unless $subname =~ /'|::/;
  213.             $subname = "main" . $subname if substr($subname,0,1)eq "'";
  214.             $subname = "main" . $subname if substr($subname,0,2)eq "::";
  215.             ($file,$subrange) = split(/:/,$sub{$subname});
  216.             if ($file ne $filename) {
  217.             *dbline = "::_<$file";
  218.             $max = $#dbline;
  219.             $filename = $file;
  220.             }
  221.             if ($subrange) {
  222.             if (eval($subrange) < -$window) {
  223.                 $subrange =~ s/-.*/+/;
  224.             }
  225.             $cmd = "l $subrange";
  226.             } else {
  227.             print OUT "Subroutine $1 not found.\n";
  228.             next CMD;
  229.             } };
  230.         $cmd =~ /^w\b\s*(\d*)$/ && do {
  231.             $incr = $window - 1;
  232.             $start = $1 if $1;
  233.             $start -= $preview;
  234.             $cmd = 'l ' . $start . '-' . ($start + $incr); };
  235.         $cmd =~ /^-$/ && do {
  236.             $incr = $window - 1;
  237.             $cmd = 'l ' . ($start-$window*2) . '+'; };
  238.         $cmd =~ /^l$/ && do {
  239.             $incr = $window - 1;
  240.             $cmd = 'l ' . $start . '-' . ($start + $incr); };
  241.         $cmd =~ /^l\b\s*(\d*)\+(\d*)$/ && do {
  242.             $start = $1 if $1;
  243.             $incr = $2;
  244.             $incr = $window - 1 unless $incr;
  245.             $cmd = 'l ' . $start . '-' . ($start + $incr); };
  246.         $cmd =~ /^l\b\s*(([\d\$\.]+)([-,]([\d\$\.]+))?)?/ && do {
  247.             $end = (!$2) ? $max : ($4 ? $4 : $2);
  248.             $end = $max if $end > $max;
  249.             $i = $2;
  250.             $i = $line if $i eq '.';
  251.             $i = 1 if $i < 1;
  252.             if ($emacs) {
  253.             print OUT "\032\032$filename:$i:0\n";
  254.             $i = $end;
  255.             } else {
  256.             for (; $i <= $end; $i++) {
  257.                 print OUT "$i:\t", $dbline[$i];
  258.                 last if $signal;
  259.             }
  260.             }
  261.             $start = $i;    # remember in case they want more
  262.             $start = $max if $start > $max;
  263.             next CMD; };
  264.         $cmd =~ /^D$/ && do {
  265.             print OUT "Deleting all breakpoints...\n";
  266.             for ($i = 1; $i <= $max ; $i++) {
  267.             if (defined $dbline{$i}) {
  268.                 $dbline{$i} =~ s/^[^\0]+//;
  269.                 if ($dbline{$i} =~ s/^\0?$//) {
  270.                 delete $dbline{$i};
  271.                 }
  272.             }
  273.             }
  274.             next CMD; };
  275.         $cmd =~ /^L$/ && do {
  276.             for ($i = 1; $i <= $max; $i++) {
  277.             if (defined $dbline{$i}) {
  278.                 print OUT "$i:\t", $dbline[$i];
  279.                 ($stop,$action) = split(/\0/, $dbline{$i});
  280.                 print OUT "  break if (", $stop, ")\n" 
  281.                 if $stop;
  282.                 print OUT "  action:  ", $action, "\n" 
  283.                 if $action;
  284.                 last if $signal;
  285.             }
  286.             }
  287.             next CMD; };
  288.         $cmd =~ /^b\b\s*([':A-Za-z_][':\w]*)\s*(.*)/ && do {
  289.             $subname = $1;
  290.             $cond = $2 || '1';
  291.             $subname = "${package}::" . $subname
  292.             unless $subname =~ /'|::/;
  293.             $subname = "main" . $subname if substr($subname,0,1) eq "'";
  294.             $subname = "main" . $subname if substr($subname,0,2) eq "::";
  295.             ($filename,$i) = split(/:/, $sub{$subname});
  296.             $i += 0;
  297.             if ($i) {
  298.             *dbline = "::_<$filename";
  299.             ++$i while $dbline[$i] == 0 && $i < $#dbline;
  300.             $dbline{$i} =~ s/^[^\0]*/$cond/;
  301.             } else {
  302.             print OUT "Subroutine $subname not found.\n";
  303.             }
  304.             next CMD; };
  305.         $cmd =~ /^b\b\s*(\d*)\s*(.*)/ && do {
  306.             $i = ($1?$1:$line);
  307.             $cond = $2 || '1';
  308.             if ($dbline[$i] == 0) {
  309.             print OUT "Line $i not breakable.\n";
  310.             } else {
  311.             $dbline{$i} =~ s/^[^\0]*/$cond/;
  312.             }
  313.             next CMD; };
  314.         $cmd =~ /^d\b\s*(\d+)?/ && do {
  315.             $i = ($1?$1:$line);
  316.             $dbline{$i} =~ s/^[^\0]*//;
  317.             delete $dbline{$i} if $dbline{$i} eq '';
  318.             next CMD; };
  319.         $cmd =~ /^A$/ && do {
  320.             for ($i = 1; $i <= $max ; $i++) {
  321.             if (defined $dbline{$i}) {
  322.                 $dbline{$i} =~ s/\0[^\0]*//;
  323.                 delete $dbline{$i} if $dbline{$i} eq '';
  324.             }
  325.             }
  326.             next CMD; };
  327.         $cmd =~ /^<\s*(.*)/ && do {
  328.             $pre = action($1);
  329.             next CMD; };
  330.         $cmd =~ /^>\s*(.*)/ && do {
  331.             $post = action($1);
  332.             next CMD; };
  333.         $cmd =~ /^a\b\s*(\d+)(\s+(.*))?/ && do {
  334.             $i = $1;
  335.             if ($dbline[$i] == 0) {
  336.             print OUT "Line $i may not have an action.\n";
  337.             } else {
  338.             $dbline{$i} =~ s/\0[^\0]*//;
  339.             $dbline{$i} .= "\0" . action($3);
  340.             }
  341.             next CMD; };
  342.         $cmd =~ /^n$/ && do {
  343.             $single = 2;
  344.             $laststep = $cmd;
  345.             last CMD; };
  346.         $cmd =~ /^s$/ && do {
  347.             $single = 1;
  348.             $laststep = $cmd;
  349.             last CMD; };
  350.         $cmd =~ /^c\b\s*(\d*)\s*$/ && do {
  351.             $i = $1;
  352.             if ($i) {
  353.             if ($dbline[$i] == 0) {
  354.                 print OUT "Line $i not breakable.\n";
  355.                 next CMD;
  356.             }
  357.             $dbline{$i} =~ s/(\0|$)/;9$1/;    # add one-time-only b.p.
  358.             }
  359.             for ($i=0; $i <= $#stack; ) {
  360.             $stack[$i++] &= ~1;
  361.             }
  362.             last CMD; };
  363.         $cmd =~ /^r$/ && do {
  364.             $stack[$#stack] |= 2;
  365.             last CMD; };
  366.         $cmd =~ /^T$/ && do {
  367.             local($p,$f,$l,$s,$h,$a,@a,@sub);
  368.             for ($i = 1; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) {
  369.             @a = ();
  370.             for $arg (@args) {
  371.                 $_ = "$arg";
  372.                 s/'/\\'/g;
  373.                 s/([^\0]*)/'$1'/
  374.                 unless /^(?: -?[\d.]+ | \*[\w:]* )$/x;
  375.                 s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
  376.                 s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
  377.                 push(@a, $_);
  378.             }
  379.             $w = $w ? '@ = ' : '$ = ';
  380.             $a = $h ? '(' . join(', ', @a) . ')' : '';
  381.             push(@sub, "$w$s$a from file $f line $l\n");
  382.             last if $signal;
  383.             }
  384.             for ($i=0; $i <= $#sub; $i++) {
  385.             last if $signal;
  386.             print OUT $sub[$i];
  387.             }
  388.             next CMD; };
  389.         $cmd =~ /^\/(.*)$/ && do {
  390.             $inpat = $1;
  391.             $inpat =~ s:([^\\])/$:$1:;
  392.             if ($inpat ne "") {
  393.             eval '$inpat =~ m'."\a$inpat\a";    
  394.             if ($@ ne "") {
  395.                 print OUT "$@";
  396.                 next CMD;
  397.             }
  398.             $pat = $inpat;
  399.             }
  400.             $end = $start;
  401.             eval '
  402.             for (;;) {
  403.             ++$start;
  404.             $start = 1 if ($start > $max);
  405.             last if ($start == $end);
  406.             if ($dbline[$start] =~ m'."\a$pat\a".'i) {
  407.                 if ($emacs) {
  408.                 print OUT "\032\032$filename:$start:0\n";
  409.                 } else {
  410.                 print OUT "$start:\t", $dbline[$start], "\n";
  411.                 }
  412.                 last;
  413.             }
  414.             } ';
  415.             print OUT "/$pat/: not found\n" if ($start == $end);
  416.             next CMD; };
  417.         $cmd =~ /^\?(.*)$/ && do {
  418.             $inpat = $1;
  419.             $inpat =~ s:([^\\])\?$:$1:;
  420.             if ($inpat ne "") {
  421.             eval '$inpat =~ m'."\a$inpat\a";    
  422.             if ($@ ne "") {
  423.                 print OUT "$@";
  424.                 next CMD;
  425.             }
  426.             $pat = $inpat;
  427.             }
  428.             $end = $start;
  429.             eval '
  430.             for (;;) {
  431.             --$start;
  432.             $start = $max if ($start <= 0);
  433.             last if ($start == $end);
  434.             if ($dbline[$start] =~ m'."\a$pat\a".'i) {
  435.                 if ($emacs) {
  436.                 print OUT "\032\032$filename:$start:0\n";
  437.                 } else {
  438.                 print OUT "$start:\t", $dbline[$start], "\n";
  439.                 }
  440.                 last;
  441.             }
  442.             } ';
  443.             print OUT "?$pat?: not found\n" if ($start == $end);
  444.             next CMD; };
  445.         $cmd =~ /^!+\s*(-)?(\d+)?$/ && do {
  446.             pop(@hist) if length($cmd) > 1;
  447.             $i = ($1?($#hist-($2?$2:1)):($2?$2:$#hist));
  448.             $cmd = $hist[$i] . "\n";
  449.             print OUT $cmd;
  450.             redo CMD; };
  451.         $cmd =~ /^!(.+)$/ && do {
  452.             $pat = "^$1";
  453.             pop(@hist) if length($cmd) > 1;
  454.             for ($i = $#hist; $i; --$i) {
  455.             last if $hist[$i] =~ $pat;
  456.             }
  457.             if (!$i) {
  458.             print OUT "No such command!\n\n";
  459.             next CMD;
  460.             }
  461.             $cmd = $hist[$i] . "\n";
  462.             print OUT $cmd;
  463.             redo CMD; };
  464.         $cmd =~ /^H\b\s*(-(\d+))?/ && do {
  465.             $end = $2?($#hist-$2):0;
  466.             $hist = 0 if $hist < 0;
  467.             for ($i=$#hist; $i>$end; $i--) {
  468.             print OUT "$i: ",$hist[$i],"\n"
  469.                 unless $hist[$i] =~ /^.?$/;
  470.             };
  471.             next CMD; };
  472.         $cmd =~ s/^p( .*)?$/print DB::OUT$1/;
  473.         $cmd =~ /^=/ && do {
  474.             if (local($k,$v) = ($cmd =~ /^=\s*(\S+)\s+(.*)/)) {
  475.             $alias{$k}="s~$k~$v~";
  476.             print OUT "$k = $v\n";
  477.             } elsif ($cmd =~ /^=\s*$/) {
  478.             foreach $k (sort keys(%alias)) {
  479.                 if (($v = $alias{$k}) =~ s~^s\~$k\~(.*)\~$~$1~) {
  480.                 print OUT "$k = $v\n";
  481.                 } else {
  482.                 print OUT "$k\t$alias{$k}\n";
  483.                 };
  484.             };
  485.             };
  486.             next CMD; };
  487.         }
  488.         $evalarg = $cmd; &eval;
  489.         print OUT "\n";
  490.     }
  491.     if ($post) {
  492.         $evalarg = $post; &eval;
  493.     }
  494.     }
  495.     ($@, $!, $,, $/, $\) = @saved;
  496. }
  497.  
  498. sub save {
  499.     @saved = ($@, $!, $,, $/, $\, $^W);
  500.     $, = ""; $/ = "\n"; $\ = ""; $^W = 0;
  501. }
  502.  
  503. # The following takes its argument via $evalarg to preserve current @_
  504.  
  505. sub eval {
  506.     eval "$usercontext $evalarg; &DB::save";
  507.     print OUT $@;
  508. }
  509.  
  510. sub action {
  511.     local($action) = @_;
  512.     while ($action =~ s/\\$//) {
  513.     print OUT "+ ";
  514.     $action .= &gets;
  515.     }
  516.     $action;
  517. }
  518.  
  519. sub gets {
  520.     local($.);
  521.     <IN>;
  522. }
  523.  
  524. sub catch {
  525.     $signal = 1;
  526. }
  527.  
  528. sub sub {
  529.     push(@stack, $single);
  530.     $single &= 1;
  531.     $single |= 4 if $#stack == $deep;
  532.     if (wantarray) {
  533.     @i = &$sub;
  534.     $single |= pop(@stack);
  535.     @i;
  536.     }
  537.     else {
  538.     $i = &$sub;
  539.     $single |= pop(@stack);
  540.     $i;
  541.     }
  542. }
  543.  
  544. $trace = $signal = $single = 0;    # uninitialized warning suppression
  545.  
  546. @hist = ('?');
  547. $SIG{'INT'} = "DB::catch";
  548. $deep = 100;        # warning if stack gets this deep
  549. $window = 10;
  550. $preview = 3;
  551.  
  552. @stack = (0);
  553. @ARGS = @ARGV;
  554. for (@args) {
  555.     s/'/\\'/g;
  556.     s/(.*)/'$1'/ unless /^-?[\d.]+$/;
  557. }
  558.  
  559. if (-f $rcfile) {
  560.     do "./$rcfile";
  561. }
  562. elsif (-f "$ENV{'LOGDIR'}/$rcfile") {
  563.     do "$ENV{'LOGDIR'}/$rcfile";
  564. }
  565. elsif (-f "$ENV{'HOME'}/$rcfile") {
  566.     do "$ENV{'HOME'}/$rcfile";
  567. }
  568.  
  569. 1;
  570.